package com.lanou.dllo.mynovel.dataBase;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.lanou.dllo.mynovel.bean.StoreRanking;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 15/11/3.
 */
public class MySQLiteRankingUtil {
    private SQLiteDatabase db;
    private Context context;

    public MySQLiteRankingUtil(Context c) {// 将activity对象传递过来
        context = c;
        initView();
    }

    private void initView() {// 调用helper类声明在哪里数据库中进行操作
        MySQLiteOpenHelper helper = new MySQLiteOpenHelper(context, Constant.DATABASE_NAME, null, 1);
        db = helper.getWritableDatabase();
    }

    public void saveData(List<StoreRanking.DataEntity.ReturnDataEntity.RankinglistEntity> list) {// 向数据库中保存数据
        for (StoreRanking.DataEntity.ReturnDataEntity.RankinglistEntity entity : list) {// 在list集合中遍历 RankinglistEntity 对象
            ContentValues values = new ContentValues();// 用来操作数据库数据的类
            values.put(Constant.COLUMN_STORERANKING_RECYCLERVIEW_COVER, entity.getCover());
            values.put(Constant.COLUMN_STORERANKING_RECYCLERVIEW_RANKINGNAME, entity.getRankingName());
            values.put(Constant.COLUMN_STORERANKING_RECYCLERVIEW_RANKINGDESCRIPTION1, entity.getRankingDescription1());
            values.put(Constant.COLUMN_STORERANKING_RECYCLERVIEW_RANKINGDESCRIPTION2, entity.getRankingDescription2());
            db.insert(Constant.TABLE_NAME_RANKING, null, values);
        }
    }

    public List<StoreRanking.DataEntity.ReturnDataEntity.RankinglistEntity> queryAll() {// 查找所有
        Cursor cursor = db.query(Constant.TABLE_NAME_RANKING, null, null, null, null, null, null);
        if (cursor != null) {
            List<StoreRanking.DataEntity.ReturnDataEntity.RankinglistEntity> entities = new ArrayList<>();
            while (cursor.moveToNext()) {
                String cover = cursor.getString(cursor.getColumnIndex(Constant.COLUMN_STORERANKING_RECYCLERVIEW_COVER));
                String rankingName = cursor.getString(cursor.getColumnIndex(Constant.COLUMN_STORERANKING_RECYCLERVIEW_RANKINGNAME));
                String rankingDescription1 = cursor.getString(cursor.getColumnIndex(Constant.COLUMN_STORERANKING_RECYCLERVIEW_RANKINGDESCRIPTION1));
                String rankingDescription2 = cursor.getString(cursor.getColumnIndex(Constant.COLUMN_STORERANKING_RECYCLERVIEW_RANKINGDESCRIPTION2));

                StoreRanking.DataEntity.ReturnDataEntity.RankinglistEntity entity = new StoreRanking.DataEntity.ReturnDataEntity.RankinglistEntity();
                entity.setCover(cover);
                entity.setRankingName(rankingName);
                entity.setRankingDescription1(rankingDescription1);
                entity.setRankingDescription2(rankingDescription2);
                entities.add(entity);
            }
            cursor.close();
            return entities;
        }
        return null;
    }
}
